// dayjs: https://dayjs.fenxianglu.cn/
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration';
dayjs.extend(duration)

// 时间格式化
export function dateFormat(date, format = "YYYY-MM-DD HH:mm:ss") {
	if (date && typeof(date) === 'number' && date.toString().length === 10) {
		date = Number(date) * 1000
	}
	return dayjs(date).format(format)
}

// 取10位unix时间戳
export function dateToUnix(dateStr) {
	return dayjs(dateStr).unix()
}

// 补齐
export function completeDateStr(dateStr) {
	if (dateStr.length === 10) {
		return `${dateStr} 00:00:00`
	}
	return dateStr
}

// 时长
export function timeDuration(time) {
	const duration = dayjs.duration(Number(time))
  const days = duration.days()
  const hours = duration.hours()
  const minutes = duration.minutes()
  const seconds = duration.seconds()
  return (days > 0 ? `${days}天`: '') + (hours > 0 ? `${hours}时`: '') + (minutes > 0 ? `${minutes}分`: '') + (seconds > 0 ? `${seconds}秒`: '')
}